<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isShow">
        <span @click="$emit('clearAll')">全部删除</span>&nbsp;&nbsp;&nbsp;
        <span @click="isShow = false">完成</span>
      </div>

      <van-icon
        v-else
        name="delete"
        @click="isShow = true"
      />
    </van-cell>

    <van-cell class="no-list" v-show="!historyList.length" title="暂无历史记录"></van-cell>

    <van-cell
      v-for="item in historyList"
      :key="item"
      :title="item"
      @click="twoFn(item)"
    >
      <van-icon v-show="isShow" name="close" />
    </van-cell>
  </div>
</template>

<script>
export default {
  props: ['historyList'],
  data() {
    return {
      isShow: false
    }
  },
  methods: {
    twoFn(item) {
      if(this.isShow) {
        // 删除
        let index = this.historyList.indexOf(item)

        // 因为这是引用数据类型 Array Object 并没有修改地址
        this.historyList.splice(index, 1)
      } else {
        // 跳转
        this.$emit('search', item)
      }
    }
  }
}
</script>

<style scoped lang="less">
.no-list {
  color: #ccc;
  text-align: center;
}
</style>
